/* container 容器,autoMargin多余空间是否自动分配给外边距(boolean 为false时自动分配给宽度),minWidth 单个div最小宽度,maxWidth(number) 单个div最大宽度(number),padding 单个div内边距(number或者string) */
export const intelligenceAdaptiveLayout = (obj: any) => {
  if (obj.container) {
    if (!obj.autoMargin) {
      obj.autoMargin = false;
    }
    if (!obj.minWidth || obj.minWidth < 0) {
      obj.minWidth = 280;
    }
    if (!obj.maxWidth || obj.maxWidth < 0) {
      obj.maxWidth = 280;
    }
    if (!obj.padding) {
      obj.padding = '1.5%';
    }
    const container = obj.container;
    const autoMargin = obj.autoMargin;
    const minWidth = obj.minWidth;
    const maxWidth = obj.maxWidth;
    const padding = obj.padding;
    const containerWidth = container.offsetWidth - 1 || 0; /* 容器宽度(number) */
    // let availableSpace = 0; /* availableSpace可用空间 */
    let handledPadding = padding; /* handledPadding 处理后的内边距(可直接参与计算) */
    let margin = 0;
    let divStyle = '';
    /* 处理padding(如果是string 换算成number) */
    if (isNaN(Number(padding))) {
      handledPadding =
        padding.indexOf('%') > -1
          ? (Number(padding.split('%')[0]) * containerWidth) / 100
          : Number(padding.split('px')[0]);
    }
    /* 单个div原始宽度 */
    const oldDivWidth = minWidth + handledPadding * 2;
    /* 每行个数 */
    let rowNumber = Math.floor((containerWidth - handledPadding * 2) / oldDivWidth);
    /* 每行个数(最少) */
    const leastRowNumber = Math.floor(
      (containerWidth - handledPadding * 2) / (maxWidth + handledPadding * 2),
    );

    /* 剩余空间 */
    let remainingSpace = containerWidth - handledPadding * 2 - oldDivWidth * rowNumber;
    if (remainingSpace < 80) {
      //剩余空间小于80 则每行显示的数量减去1  这样就能分配更多的剩余空间   解决特定窗口宽度 margin过小的问题
      rowNumber = rowNumber - 1;
      remainingSpace = containerWidth - handledPadding * 2 - oldDivWidth * rowNumber;
    }
    /* 最小剩余空间 */
    const minRemainingSpace =
      containerWidth - handledPadding * 2 - leastRowNumber * (maxWidth + handledPadding * 2);
    /* 单个div单边可扩展空间 */
    const divUnilateralScalableSpace = remainingSpace / (rowNumber + 1) / 2;
    /* 单个div单边最小可扩展空间 */
    const minDivUnilateralScalableSpace = minRemainingSpace / (leastRowNumber + 1) / 2;
    /* 剩余空间自动分配给外边距的全套样式 */
    if (autoMargin) {
      container.style = `display:flex;align-items:center;flex-wrap:wrap;padding:0 ${
        divUnilateralScalableSpace + handledPadding
      }px`;
      divStyle = `width:${minWidth}px;margin:${divUnilateralScalableSpace}px`;
    } else {
      /* 剩余空间自动分配给宽度  且最终div的宽度大于最大宽度的全套样式 */
      if (divUnilateralScalableSpace * 2 + minWidth >= maxWidth) {
        container.style = `display:flex;align-items:center;flex-wrap:wrap;padding:0 ${
          minDivUnilateralScalableSpace + handledPadding
        }px`;
        divStyle = `width:${maxWidth}px;margin:${minDivUnilateralScalableSpace}px`;
      } else {
        margin =
          (containerWidth -
            (divUnilateralScalableSpace * 2 + minWidth + handledPadding * 2) * rowNumber -
            handledPadding * 2) /
          (rowNumber + 1) /
          2;
        /* 剩余空间自动分配给宽度  且最终div的宽度小于于最大宽度的全套样式 */
        container.style = `display:flex;align-items:center;flex-wrap:wrap;padding:0 ${
          handledPadding + margin
        }px`;
        divStyle = `width:${
          divUnilateralScalableSpace * 2 + minWidth
        }px; padding:${handledPadding}px;margin:${margin}`;
      }
    }
    return divStyle;
  } else {
    return null;
  }
};

/**
 * 获取label
 * @code 报警code
 * */
export const getAlarmLabel = (code: any) => {
  const emun = [
    {name: '体动', code: 80},
    {name: '打鼾', code: 7010},
    {name: '在离线', code: 7010},
    {name: 'SOS', code: 30},
    {name: '心率', code: 1004},
    {name: '呼吸', code: 1005},
    {name: '体温', code: 1006},
    {name: '血压', code: 1110},
    {name: '血糖', code: 1121},
    {name: '血氧', code: 1120},
    {name: '记步', code: 1200},
    {name: '位置（经纬度）', code: 1201},
    {name: '信号强度', code: 1202},
    {name: '电量值', code: 1203},
    {name: '正常佩戴', code: 7000},
    {name: '未佩戴', code: 7001},
    {name: '手环关机（离线）', code: 1300},
    {name: '手环在线', code: 1300},
    {name: '烟雾报警', code: 60},
    {name: '低压报警', code: 384},
    {name: '燃气报警', code: 50},
    {name: '门打开', code: 989},
    {name: '门关闭', code: 988},
    {name: '水浸报警', code: 40},
    {name: '防拆报警', code: 963},
    {name: '紧急报警', code: 30},
    {name: '红外低压', code: 940},
    {name: '红外离线', code: 939},
    {name: '红外报警', code: 941},
    {name: '探测区域已无人', code: 935},
    {name: '探测区域有人', code: 936},
    {name: '电源故障', code: 7006},
    {name: '电源恢复正常', code: 7007},
    {name: '心率偏低', code: 4},
    {name: '心率偏高', code: 41},
    {name: '呼吸偏低', code: 5},
    {name: '呼吸偏高', code: 51},
    {name: '体温偏低', code: 6},
    {name: '体温偏高', code: 61},
    {name: '翻身报警', code: 1},
    {name: '尿失禁报警', code: 3},
    {name: '坠床预警', code: 70},
    {name: '坐床边', code: 71},
    {name: '恢复正常', code: 90},
    {name: '紧急异常', code: 103},
    {name: '低血压报警', code: 110},
    {name: '高血压报警', code: 111},
    {name: '低血糖', code: 121},
    {name: '高血糖', code: 122},
    {name: '低血氧', code: 120},
    {name: '高血氧', code: 123},
    {name: 'SOS报警', code: 30},
    {name: '电子围栏', code: 1030},
  ];
  return (emun.find((_) => code === _.code) || {}).name;
};
